package info.openlvb.openface.client.handler;

import info.openlvb.openface.client.Openface;
import info.openlvb.openface.client.base.Account;
import info.openlvb.openface.client.base.backup.BackupPanel;
import info.openlvb.openface.client.base.home.HomePanel;
import info.openlvb.openface.client.base.machine.MachinePanel;
import info.openlvb.openface.client.base.person.Personpanel;
import info.openlvb.openface.client.images.OpenFaceImages;
import info.openlvb.openface.client.service.LdapService;
import info.openlvb.openface.client.service.LdapServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.gwtext.client.widgets.MessageBox;


public class Login_Ok_Handler implements ClickHandler, KeyUpHandler{

	private Openface openFace;
	
	private final LdapServiceAsync ldapService = GWT
	.create(LdapService.class);

	OpenFaceImages images = GWT.create(OpenFaceImages.class);

	public Login_Ok_Handler(Openface openFace){ //RootPanel rootPanel, String login, String password){
		this.openFace = openFace;
	}
	public void onClick(ClickEvent event) {
		authentification();	
	}
	public void onKeyUp(KeyUpEvent event) {
		if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
			authentification();
		}
	}

	private void authentification() {
		ldapService.login(openFace.getLogin(), openFace.getPassword(), 
				new AsyncCallback<Account>() {

					public void onFailure(Throwable caught) {
						final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
						simplePopup.setWidget(new HTML("probleme d'authentification"));
						simplePopup.center();
						simplePopup.show();
					}

					
					public void onSuccess(Account result) {
						if(result==null){
							MessageBox.alert("L'authentification a échouée");
						}else{
							openFace.getRootPanel().clear();
							/** Modification : Xavier
							 *  Remplacement du DockPanel par un FlowPanel
							 */							
							//DockPanel hp = new DockPanel();
							FlowPanel hp = new FlowPanel();
							/** Modification : Xavier
							 *  hp.setSize impose la hauteur et la largeur
							 */
							//hp.setWidth("100%");
							hp.setSize("100%", "20%");
							//hp.setHeight("100%");
							//hp.add(images.openlvb().createImage(), DockPanel.WEST);
							hp.add(images.openlvb().createImage());
							/** Modification : Xavier
							 *  Suppression du Flowpanel et ajout du code HTML
							 */
							//FlowPanel loginpanel= new FlowPanel();
							//loginpanel.add(new HTML("<h3>"+result.getLogin()));
							//hp.add(loginpanel,DockPanel.EAST);
							//hp.add(new HTML("<div id=\"connected\">Connected : "+openFace.getLogin()+" | <a href=\"\">log out</a></div><hr>"),DockPanel.EAST);
							hp.add(new HTML("<div id=\"connected\">Connected : "+openFace.getLogin()+" | <a href=\"\">log out</a></div><hr>"));
							/** Modification : Xavier
							 *  Changement pour un ScrollPanel au lieu de VerticalPanel.
							 *  Permet de scroller lorsque tout est developpe. 
							 */
							//VerticalPanel vp = new VerticalPanel();
							ScrollPanel vp = new ScrollPanel();
							/** Modification : Xavier
							 *  Passage de la largeur de 120% à 100%
							 *  Ajout d'espace
							 */
							//vp.setWidth("120%");
							vp.setSize("100%","80%");
							vp.setStyleName("VP");
							/** Modification : Xavier
							 *  Suppression de decPanel qui est superflue
							 */
							//DecoratorPanel decPanel = new DecoratorPanel();
							//decPanel.setWidth("90%");
							DecoratedTabPanel tabPanel = new DecoratedTabPanel();
							/** Modification : Xavier
							 *  Modification de la largeur du tabPanel
							 */
							//tabPanel.setWidth("100%");
							tabPanel.setWidth("99%");
							tabPanel.setStyleName("TabPanel");
							//tabPanel.setHeight("100%");
							tabPanel.setAnimationEnabled(true);
							tabPanel.add(new HomePanel(), "Accueil");
							tabPanel.add(new Personpanel(),"Utilisateurs et groupes");
							tabPanel.add(new MachinePanel(), "Ordinateurs");
							tabPanel.add(new BackupPanel(), "Sauvegarde");
							tabPanel.selectTab(0);
							//decPanel.setWidget(tabPanel);
							vp.add(tabPanel);
							/** Modification : Xavier
							 *  Ajout de hp dans le RootPanel plutôt que dans le vp. 
							 */
							//vp.add(hp);
							//vp.add(decPanel);
							openFace.getRootPanel().add(hp);
							openFace.getRootPanel().add(vp);
						}
					}});
	}
}
